package com.sdp.right.db.sys_role_person.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.db.IViewQuery;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.right.db.sys_role_person.mapper.SysRolePersonMapper;
import com.sdp.right.db.sys_role_person.mapper.SysRolePersonViewMapper;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.entity.SysRolePerson;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-12
 */
@Service
@DS(value="right")
public class SysRolePersonServiceImpl extends ServiceImpl<SysRolePersonMapper, SysRolePerson> implements ISysRolePersonService ,IViewQuery{

	@Autowired
	private SysRolePersonViewMapper sysRolePersonViewMapper;
	
	@Override
	public List<SysRolePerson> querySysRolePersonByAccountNo(String accountNo) {
		LambdaQueryWrapper<SysRolePerson> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysRolePerson::getAccountNo, accountNo);
		return list(queryWrapper);
	}

	@Override
	public List<String> queryRoleNos(String accountNo) {
		List<String> roleNoList = new ArrayList<>();
		List<SysRolePerson> roleNos = querySysRolePersonByAccountNo(accountNo);
		if(roleNos==null || roleNos.size()==0) {
			return roleNoList;
		}
		for(SysRolePerson rp : roleNos) {
			roleNoList.add(rp.getRoleNo());
		}
		return roleNoList;
	}

	@Override
	public void removeSysRolePersonByRoleNo(String roleNo) {
		LambdaQueryWrapper<SysRolePerson> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysRolePerson::getRoleNo, roleNo);
		remove(queryWrapper);
	}

	@Override
	public <T> List<T> viewList(QueryWrapper<T> queryWrapper,DataCenter dc) {
		return sysRolePersonViewMapper.viewList(queryWrapper,dc);
	}

	@Override
	public <T> int viewCount(QueryWrapper<T> queryWrapper) {
		return sysRolePersonViewMapper.viewCount(queryWrapper);
	}

	@Override
	public <T> IPage<T> viewPage(IPage<T> page, QueryWrapper<T> queryWrapper,DataCenter dc) {
		return sysRolePersonViewMapper.viewPage(page, queryWrapper,dc);
	}

	@Override
	public List<SysRolePerson> querySysRolePersonByRoleNo(String roleNo) {
		LambdaQueryWrapper<SysRolePerson> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysRolePerson::getRoleNo, roleNo);
		return list(queryWrapper);
	}

	@Override
	public void removeSysRolePersonByAccountNo(String accountNo) {
		LambdaQueryWrapper<SysRolePerson> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysRolePerson::getAccountNo, accountNo);
		remove(queryWrapper);
		
	}

	@Override
	public void addRolePerson(SysEmp sysEmp, String roleNo) {
		SysRolePerson rp = new SysRolePerson();
		rp.setAccountNo(sysEmp.getAccountNo());
		rp.setRoleNo(roleNo);
		rp.setCreateTime(LocalDateTime.now());
		rp.setRolePersonSort(1);
		save(rp);
		
	}

}
